/**
 * 期费用预提 - 编辑页面
 * @author shixc
 * @date 2019/10/25
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            AMOUNT: '',
            RATE: '',
            isAdd: false,
            isCheck:false,
            form: '',
            total: {
                GWFC102_SUM: 0,
                GWFC106_SUM: 0,
                GWFC107_SUM: 0
            },
            rules: {
                GWFCDOCNO: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFC001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFC002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions: {
                gwdc007Options: app.datas_('crm_gwcd_gwcd007')
            }
        },
        cols = [[
            {field: 'GWFCSEQ', type: 'checkbox', title: '选择'}
            , {field: 'GWFCSEQ', width: 80, title: '项次'}
            , {field: 'GWFC003_NAME', width: 80, title: '区域', event: 'openGwad'}
            , {field: 'GWFC004_NAME', width: 120, title: '客户', event: 'openGwak'}
            , {field: 'GWFC005_NAME', width: 120, title: '品类', event: 'openGwao'}
            , {field: 'GWFC006_NAME', width: 120, title: '业务组织', event: 'openOrg'}
            , {field: 'GWFC007_NAME', width: 80, title: '费用', event: 'openGwfa'}
            , {field: 'GWFC100_NAME', width: 80, title: '币别', event: 'openGwac'}
            , {field: 'GWFC101', width: 80, title: '汇率', edit: 'text'}
            , {field: 'GWFC102', width: 180, title: '预测销售原币金额', edit: 'text'}
            , {field: 'GWFC103', width: 180, title: '预测销售本币金额'}
            , {field: 'GWFC104', width: 80, title: '区域费率'}
            , {field: 'GWFC105', width: 80, title: '总部费率'}
            , {field: 'GWFC106', width: 180, title: '区域预提费用原币金额', edit: 'text'}
            , {field: 'GWFC107', width: 180, title: '区域预提费用本币金额'}
            , {field: 'GWFC108', width: 180, title: '总部预提费用原币金额', edit: 'text'}
            , {field: 'GWFC109', width: 180, title: '总部预提费用本币金额'}
            , {field: 'GWFCACTI', width: 80, title: '有效'}
            , {field: 'GWFC003', width: 80, title: '单位说明', hide: true}
            , {field: 'GWFC004', width: 80, title: '单位说明', hide: true}
            , {field: 'GWFC005', width: 80, title: '单位说明', hide: true}
            , {field: 'GWFC006', width: 80, title: '单位说明', hide: true}
            , {field: 'GWFC007', width: 80, title: '单位说明', hide: true}
            , {field: 'GWFC100', width: 80, title: '单位说明', hide: true}
        ]],
        callObj,
        moduleName,
        listData = null,
        pageVue,
        table, gwdc, gwdbTable, layOpenIndex
    ;

    var Gwft200Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwft200_edit',
                    _loadTplsFull: [
                        {dom: 'common_open_window', tpl: 'common/_open_window'}
                    ]
                }, win);
            },
            initTable: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwdbTable = table.render({
                        elem: app.getByid(doc, 'gwft200_edit_table'),
                        id: 'gwft200_edit_table',
                        // size: _layuiConfig.ui_size,
                        // autoSort: false, //禁用前端自动排序
                        cols: cols,
                        data: listData,
                        //loading: true,
                        page: false,
                        height: 300,
                        where: '',
                        // done: function (res, curr, count) {
                        // }
                    });

                    table.on('tool(gwft200_edit_table)', function (obj) {
                        if (obj.event === 'openGwad') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwad002',
                                        columnList: [
                                            {label: '区域编号', prop: 'GWAD002', width: '150'},
                                            {label: '区域名称', prop: 'GWADL004', width: '150'},
                                            {label: '助记码', prop: 'GWADL005', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            'GWFC003': data.GWAD002,
                                            'GWFC003_NAME': data.GWADL004
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择区域',
                                    area: ['600px', '465px']
                                }
                            });
                        } else if (obj.event == 'openGwak') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwak001',
                                        columnList: [
                                            {label: '客户编号', prop: 'GWAK001', width: '150'},
                                            {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                            {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            'GWFC004': data.GWAK001,
                                            'GWFC004_NAME': data.GWAKL003,
                                            'GWFC100': data.GWAK008,
                                            'GWFC100_NAME': data.GWAK008_NAME
                                        });
                                        app.ajax({
                                            type: 'POST',
                                            dataType: 'json',
                                            xurl: 'crm/gwft200/getRate',
                                            xdata: {
                                                GWEK001: data.GWAK008,
                                                GWEK002: (vueData.form.GWFD001 + vueData.form.GWFD002)
                                            },
                                            cb: function (result) {
                                                if (result.ok) {
                                                    var GWFC101 = self.getDigit(result.data.data.RATE, vueData.RATE);
                                                    obj.updateOfEdit({
                                                        'GWFC101': GWFC101
                                                    });
                                                }
                                            }
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择客户',
                                    area: ['600px', '520px']
                                }
                            });
                        } else if (obj.event == 'openGwao') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwao001',
                                        columnList: [
                                            {label: '产品系列编号', prop: 'GWAO001', width: '150'},
                                            {label: '说明', prop: 'GWAOL003', width: '150'},
                                            {label: '助记码', prop: 'GWAOL004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            'GWFC005': data.GWAO001,
                                            'GWFC005_NAME': data.GWAOL003
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择品类',
                                    area: ['600px', '465px']
                                }
                            });
                        } else if (obj.event == 'openOrg') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysorg_id',
                                        columnList: [
                                            {label: '部门编号', prop: 'ID', width: '150'},
                                            {label: '部门名称', prop: 'NAME', width: '150'}
                                        ],
                                        searchParam:{
                                            TYPE: '3'
                                        }
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            'GWFC006': data.ID,
                                            'GWFC006_NAME': data.NAME
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择业务组织',
                                    area: ['600px', '465px']
                                }
                            });
                        } else if (obj.event == 'openGwfa') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwfa001',
                                        columnList: [
                                            {label: '费用编号', prop: 'GWFA001', width: '150'},
                                            {label: '费用说明', prop: 'GWFAL003', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            'GWFC007': data.GWFA001,
                                            'GWFC007_NAME': data.GWFAL003,
                                            'GWFC104': data.GWFA004,
                                            'GWFC105': data.GWFA005
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择费用',
                                    area: ['600px', '465px']
                                }
                            });
                        } else if (obj.event == 'openGwac') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwac001',
                                        columnList: [
                                            {label: '币种编号', prop: 'GWAC001', width: '150'},
                                            {label: '说明', prop: 'GWACL003', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            'GWFC100': data.GWAC001,
                                            'GWFC100_NAME': data.GWACL003
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择币种',
                                    area: ['600px', '465px']
                                }
                            });
                        }
                        self.calTotal(gwdbTable.getDatas());
                    });
                    // //监听单元格编辑
                    table.on('edit(gwft200_edit_table)', function (obj) {
                        if (obj.field == 'GWFC101') {
                            if (!(/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    'GWFC101': ''
                                });
                                app.showError('必需是大于0的数字');
                                return;
                            } else {
                                var GWFC101 = self.getDigit(obj.value, vueData.RATE);
                                var GWFC103 = self.getDigit(math.mul(obj.value, obj.data.GWFC102), vueData.RATE);
                                var GWFC107 = self.getDigit(math.mul(GWFC103, obj.data.GWFC104), vueData.AMOUNT);
                                var GWFC109 = self.getDigit(math.mul(GWFC103, obj.data.GWFC105), vueData.AMOUNT);
                                obj.updateOfEdit({
                                    'GWFC101': GWFC101,
                                    'GWFC103': GWFC103,
                                    'GWFC107': GWFC107,
                                    'GWFC109': GWFC109
                                });
                            }
                        } else if (obj.field == 'GWFC102') {
                            if (!(/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    'GWFC102': ''
                                });
                                app.showError('必需是大于0的数字');
                                return;
                            } else {
                                var GWFC102 = self.getDigit(obj.value, vueData.AMOUNT);
                                var GWFC103 = self.getDigit(math.mul(obj.value, obj.data.GWFC101), vueData.AMOUNT);
                                var GWFC106 = self.getDigit(math.mul(GWFC102, obj.data.GWFC104), vueData.AMOUNT);
                                var GWFC107 = self.getDigit(math.mul(GWFC103, obj.data.GWFC104), vueData.AMOUNT);
                                var GWFC108 = self.getDigit(math.mul(GWFC102, obj.data.GWFC105), vueData.AMOUNT);
                                var GWFC109 = self.getDigit(math.mul(GWFC103, obj.data.GWFC105), vueData.AMOUNT);
                                obj.updateOfEdit({
                                    'GWFC102': GWFC102,
                                    'GWFC103': GWFC103,
                                    'GWFC106': GWFC106,
                                    'GWFC107': GWFC107,
                                    'GWFC108': GWFC108,
                                    'GWFC109': GWFC109
                                });
                            }
                        }
                        // else if (obj.field == 'GWFC103') {
                        //     if (!(/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/).test(obj.value)) {
                        //         obj.updateOfEdit({
                        //             'GWFC103': ''
                        //         });
                        //         app.showError('必需是大于0的数字');
                        //         return;
                        //     } else {
                        //         var GWFC103 = self.getDigit(obj.value, vueData.AMOUNT);
                        //         var GWFC107 = self.getDigit(math.mul(GWFC103, obj.data.GWFC104), vueData.AMOUNT);
                        //         var GWFC109 = self.getDigit(math.mul(GWFC103, obj.data.GWFC105), vueData.AMOUNT);
                        //         obj.updateOfEdit({
                        //             'GWFC103': GWFC103,
                        //             'GWFC107': GWFC107,
                        //             'GWFC109': GWFC109
                        //         });
                        //     }
                        // }
                        // else if (obj.field == 'GWFC104') {
                        //     if (!(/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/).test(obj.value)) {
                        //         obj.updateOfEdit({
                        //             'GWFC104': ''
                        //         });
                        //         app.showError('必需是大于0的数字');
                        //         return;
                        //     } else {
                        //         var GWFC104 = self.getDigit(obj.value, vueData.AMOUNT);
                        //         var GWFC106 = self.getDigit(math.mul(GWFC104, obj.data.GWFC102), vueData.AMOUNT);
                        //         var GWFC107 = self.getDigit(math.mul(GWFC104, obj.data.GWFC103), vueData.AMOUNT);
                        //         obj.updateOfEdit({
                        //             'GWFC104': GWFC104,
                        //             'GWFC106': GWFC106,
                        //             'GWFC107': GWFC107
                        //         });
                        //     }
                        // } else if (obj.field == 'GWFC105') {
                        //     if (!(/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/).test(obj.value)) {
                        //         obj.updateOfEdit({
                        //             'GWFC105': ''
                        //         });
                        //         app.showError('必需是大于0的数字');
                        //         return;
                        //     } else {
                        //         var GWFC105 = self.getDigit(obj.value, vueData.AMOUNT);
                        //         var GWFC108 = self.getDigit(math.mul(GWFC105, obj.data.GWFC102), vueData.AMOUNT);
                        //         var GWFC109 = self.getDigit(math.mul(GWFC105, obj.data.GWFC103), vueData.AMOUNT);
                        //         obj.updateOfEdit({
                        //             'GWFC105': GWFC105,
                        //             'GWFC108': GWFC108,
                        //             'GWFC109': GWFC109
                        //         });
                        //     }
                        // }
                        else if (obj.field == 'GWFC106') {
                            if (!(/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    'GWFC106': ''
                                });
                                app.showError('必需是大于0的数字');
                                return;
                            } else {
                                obj.updateOfEdit({
                                    'GWFC106': self.getDigit(obj.value, vueData.AMOUNT)
                                });
                            }
                        }
                        // else if (obj.field == 'GWFC107') {
                        //     if (!(/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/).test(obj.value)) {
                        //         obj.updateOfEdit({
                        //             'GWFC107': ''
                        //         });
                        //         app.showError('必需是大于0的数字');
                        //         return;
                        //     } else {
                        //         obj.updateOfEdit({
                        //             'GWFC107': self.getDigit(obj.value, vueData.AMOUNT)
                        //         });
                        //     }
                        // }
                        else if (obj.field == 'GWFC108') {
                            if (!(/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    'GWFC108': ''
                                });
                                app.showError('必需是大于0的数字');
                                return;
                            } else {
                                obj.updateOfEdit({
                                    'GWFC108': self.getDigit(obj.value, vueData.AMOUNT)
                                });
                            }
                        }
                        // else if (obj.field == 'GWFC109') {
                        //     if (!(/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/).test(obj.value)) {
                        //         obj.updateOfEdit({
                        //             'GWFC109': ''
                        //         });
                        //         app.showError('必需是大于0的数字');
                        //         return;
                        //     } else {
                        //         obj.updateOfEdit({
                        //             'GWFC109': self.getDigit(obj.value, vueData.AMOUNT)
                        //         });
                        //     }
                        // }
                        self.calTotal(gwdbTable.getDatas());
                    });
                });
            },

            tableReload: function (data) {
                table.reload('gwft200_edit_table', {
                    data: data
                });
            },

            addData: function (listData) {
                app.ajax({
                    xurl: 'crm/gwft200/add',
                    xdata: listData,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '期费用预提增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwft200').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },
            editData: function (listData) {
                app.ajax({
                    xurl: 'crm/gwft200/edit',
                    xdata: listData,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '期费用预提修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwft200').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            getDigit: function (num, digit) {
                return math.div(Math.round(math.mul(num, digit)), digit);
            },

            calTotal: function (listData) {
                var total_data = {
                    GWFC102_SUM: 0,
                    GWFC106_SUM: 0,
                    GWFC107_SUM: 0
                };
                listData.forEach(function (item) {
                    total_data.GWFC102_SUM = math.add(total_data.GWFC102_SUM, item.GWFC102);
                    total_data.GWFC106_SUM = math.add(total_data.GWFC106_SUM, item.GWFC106);
                    total_data.GWFC107_SUM = math.add(total_data.GWFC107_SUM, item.GWFC107);
                });
                app.extendUpLow({
                    target: vueData.total,
                    res: total_data,
                    reset: true
                })
            },

            _call: function (tagData) {
                pageVue.$refs['form'].resetFields();
                if (tagData.isCheck) {
                    vueData.isCheck = true;
                } else {
                    vueData.isCheck = false;
                }
                //moduleName = tagData.moduleName;
                layOpenIndex = tagData.layOpenIndex;
                if (tagData.is_add) {
                    vueData.isAdd = true;
                    app.ajax({
                        xurl: 'crm/gwft200/getGwfcdocno',
                        cb: function (result) {
                            if (result.ok) {
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: {
                                        GWFCDOCNO: result.data.data.GWFCDOCNO
                                    },
                                    reset: true
                                });
                                listData = [];
                                self.tableReload(listData);
                            }
                        }
                    });
                } else {
                    vueData.isAdd = false;
                    callObj = tagData.obj.data;
                    app.extendUpLow({
                        target: vueData.form,
                        res: callObj,
                        reset: true
                    });
                    app.ajax({
                        xurl: 'crm/gwft200/listGwfc',
                        xdata: {GWFCDOCNO: callObj.GWFCDOCNO},
                        loading: true,
                        cb: function (result) {
                            if (result.ok) {
                                listData = result.data.data;
                                self.tableReload(listData);
                                self.calTotal(listData);
                            }
                        }
                    });
                }
                app.ajax({
                    xurl: 'admin/getDigit',
                    cb: function (result) {
                        if (result.ok) {
                            var data = result.data.data;
                            vueData.AMOUNT = Math.pow(10, data.amount);
                            vueData.RATE = Math.pow(10, data.rate);
                        } else {
                            console.log("获取小数位失败");
                        }
                    }
                });

            },
            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['GWFC_T'], {
                    GWAML003: null, GWAML004: null, GWDB007_NAME: null, GWDB008_NAME: null,
                    GWDB009_NAME: null
                });
                pageVue = new Vue({
                    el: '#gwft200_edit',
                    data: vueData,
                    watch: {},
                    methods: {
                        addData: function () {
                            if (vueData.form.GWFC001 == '' || vueData.form.GWFC001 == null) {
                                app.showWarning("请先选择年度！");
                                return;
                            } else if (vueData.form.GWFC002 == '' || vueData.form.GWFC002 == null) {
                                app.showWarning("请先选择月份！");
                                return;
                            } else {
                                var data = gwdbTable.getDatas();
                                var count = data.length;
                                data.push({
                                    GWFCSEQ: count + 1,
                                    GWFC003_NAME: '',
                                    GWFC004_NAME: '',
                                    GWFC005_NAME: '',
                                    GWFC006_NAME: '',
                                    GWFC007_NAME: '',
                                    GWFC100_NAME: '',
                                    GWFC101: '',
                                    GWFC102: '',
                                    GWFC103: '',
                                    GWFC104: '0',
                                    GWFC105: '0',
                                    GWFC106: '',
                                    GWFC107: '',
                                    GWFC108: '',
                                    GWFC109: '',
                                    GWFCACTI: 'N',
                                    GWFC003: '',
                                    GWFC004: '',
                                    GWFC005: '',
                                    GWFC006: '',
                                    GWFC007: '',
                                    GWFC100: ''
                                });
                                self.tableReload(data);
                            }
                        },
                        cancle: function () {
                            layer.close(layOpenIndex);
                        },
                        del: function () {
                            var list = gwdbTable.getDatas();
                            var checkStatus = table.checkStatus('gwft200_edit_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        GWFCSEQ: v.GWFCSEQ
                                    });
                                });
                                if (list.length != 0) {
                                    var i = 1;
                                    list.forEach(function (v) {
                                        v.GWFCSEQ = i;
                                        i++;
                                    });
                                }
                                self.tableReload(list);
                            }
                        },
                        saveData: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    var list = gwdbTable.getDatas();
                                    if (list.length == 0) {
                                        app.showError('信息不可为空!');
                                        return;
                                    }
                                    // for (var i = 0; i < list.length; i++) {
                                    //     var v = list[i];
                                    //     if (v.GWDB012 == '' || v.GWDB012 == null
                                    //         || v.GWDB014 == '' || v.GWDB014 == null) {
                                    //         app.showWarning('请选择客户及商品!');
                                    //         return;
                                    //     }
                                    //     if (v.GWDB016 == '' || v.GWDB016 == null) {
                                    //         app.showWarning('结算价必填!');
                                    //         return;
                                    //     }
                                    // }
                                    list.addMap(vueData.form, false);
                                    if (vueData.isAdd) {
                                        self.addData(list);
                                    } else {
                                        self.editData(list);
                                    }
                                }
                            });
                        },

                        openUser: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysuser_id',
                                        columnList: [{label: '申请人员编号', prop: 'ID', width: '150'},
                                            {label: '申请人员姓名', prop: 'NAME', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.GWDB007 = data.ID;
                                        vueData.form.GWDB007_NAME = data.NAME;
                                        vueData.form.GWDB008 = data.ORG_ID;
                                        vueData.form.GWDB008_NAME = data.NAME_ORG;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择申请人员',
                                    area: ['600px', '470px']
                                }
                            });
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwft200_edit');
                    }
                });
                self.initTable();
            },
            _lazy: function () {
                this._super();
            }
        })
    ;

    return new Gwft200Edit();

})
;
